package zym.diandian.tools;


import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class DisplayHelper {

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) ((dpValue * scale) + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) ((pxValue / scale) + 0.5f);
    }

    /**
     * 获取WIFI ip
     *
     * @param context
     * @return
     */
    public static String getLocalIpAddress(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        // 获取32位整型IP地址
        int ipAddress = wifiInfo.getIpAddress();

        // 返回整型地址转换成“*.*.*.*”地址
        return String.format("%d.%d.%d.%d", (ipAddress & 0xff), ((ipAddress >> 8) & 0xff), ((ipAddress >> 16) & 0xff), ((ipAddress >> 24) & 0xff));
    }

    public static String getProp(String value) {
        String result = "";
        try {
            Process process = Runtime.getRuntime().exec("getprop " + value);
            result = NetUtil.inputStream2String(process.getInputStream());
            process.destroy();
        } catch (Exception e) {
            result = "0";
            e.printStackTrace();
        }

        return result.trim();
    }

    /**
     * 获取分辨率
     *
     * @param context
     * @return
     */
    public static int[] getResolution(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int[] result = new int[2];
        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(mDisplayMetrics);
        int W = mDisplayMetrics.widthPixels;
        int H = mDisplayMetrics.heightPixels;
        result[0] = W;
        result[1] = H;
        return result;
    }

    /**
     * 获取系统SDK版本
     *
     * @return
     */
    public static int getSDKVersion() {
        return Build.VERSION.SDK_INT;
    }

    /***
     * 获取桌面壁纸
     *
     * @param context
     * @return
     */
    public static Drawable getWallpaper(Context context) {
        WallpaperManager manager = WallpaperManager.getInstance(context);
        return manager.getDrawable();
    }

//    /** 是否已开启无线调试模式
//     *
//     * @return
//     * @throws IOException */
//    private static boolean isWXTSON() throws IOException {
//        boolean isOpend = false;
//        Process pro = Runtime.getRuntime().exec("getprop service.adb.tcp.port");
//        InputStream is = pro.getInputStream();
//        String strPort = NetworkHelper.inputStream2String(is);
//        try {
//            if (TextUtils.isEmpty(strPort.trim())) {
//                isOpend = false;
//            } else {
//                LogTool.out("无限调试 端口 " + strPort);
//                int port = Integer.parseInt(strPort.trim());
//                if (port <= 0) {
//                    isOpend = false;
//                } else {
//                    //					mPort = port;
//                    isOpend = true;
//                }
//            }
//        } catch (Exception e) {
//            LogTool.out("无限调试 端口 2 " + strPort);
//            LogTool.out(LogTool.E, e);
//            isOpend = false;
//        }
//        return isOpend;
//    }

    /**
     * get screen size , need network query
     *
     * @return inches
     */
    public static String getScreenSize(Activity act) {
        String result;

        DisplayMetrics dm = new DisplayMetrics();
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
        double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
        double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
        double screenInches = Math.sqrt(x + y);
        screenInches = (double) Math.round(screenInches * 10) / 10;
        result = screenInches + " inches";
        return result;
    }

    /**
     * 获取屏幕密度
     *
     * @param act
     * @return dpi ,320,480
     */
    public static String getDensityDpi(Activity act) {
        WindowManager manager = act.getWindowManager();
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);

        return "" + metrics.densityDpi + " dpi";
    }

    /**
     * 判断设备是否支持多点触控
     *
     * @param context
     * @return
     */
    public static String isSupportMultiTouch(Context context) {
        PackageManager pm = context.getPackageManager();
        boolean isSupportMultiTouch = pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
        if (isSupportMultiTouch) {
            return "支持";
        } else {
            return "不支持";
        }
    }
}
